package today.rocky.memory.util;

import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.exception.FdfsUnsupportStorePathException;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.PostConstruct;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.Charset;

/**
 * @author Rocky
 * @version 1.0
 * @date 2020/3/4 16:34
 */
@Component
public class FastDFSClient {


	private FastFileStorageClient storageClient;

	@Value("${fdfs.tracker-list[0]}")
	private String tracker;


	@Autowired
	public FastDFSClient(FastFileStorageClient storageClient){
		this.storageClient = storageClient;
	}

	/**
	 * 上传文件
	 * @param file 文件对象
	 * @return 文件访问地址
	 * @throws IOException 上传异常
	 */
	public String uploadFile(MultipartFile file) throws IOException {
		StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(),
				FilenameUtils.getExtension(file.getOriginalFilename()), null);
		return "http://"+tracker.substring(0,tracker.indexOf(":"))+"/"+storePath.getFullPath();
	}
	
	public String uploadFile2(MultipartFile file) throws IOException {
		StorePath storePath = storageClient.uploadImageAndCrtThumbImage(file.getInputStream(), file.getSize(),
				FilenameUtils.getExtension(file.getOriginalFilename()), null);
		return "http://"+tracker.substring(0,tracker.indexOf(":"))+"/"+storePath.getFullPath();
	}
	
	public String uploadQRCode(MultipartFile file) throws IOException {
		StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(),
				"png", null);
		
		return "http://"+tracker.substring(0,tracker.indexOf(":"))+"/"+storePath.getFullPath();
	}
	
	public String uploadFace(MultipartFile file) throws IOException {
		StorePath storePath = storageClient.uploadImageAndCrtThumbImage(file.getInputStream(), file.getSize(),
				"png", null);
		
		return "http://"+tracker.substring(0,tracker.indexOf(":"))+"/"+storePath.getFullPath();
	}
	
	public String uploadBase64(MultipartFile file) throws IOException {
		StorePath storePath = storageClient.uploadImageAndCrtThumbImage(file.getInputStream(), file.getSize(),
				"png", null);

		return "http://"+tracker.substring(0,tracker.indexOf(":"))+"/"+storePath.getFullPath();
	}
	
	/**
	 * 将一段字符串生成一个文件上传
	 * @param content 文件内容
	 * @param fileExtension 扩展名
	 * @return res
	 */
	public String uploadFile(String content, String fileExtension) {
		byte[] buff = content.getBytes(Charset.forName("UTF-8"));
		ByteArrayInputStream stream = new ByteArrayInputStream(buff);
		StorePath storePath = storageClient.uploadFile(stream, buff.length, fileExtension, null);
		return "http://"+tracker.substring(0,tracker.indexOf(":"))+"/"+storePath.getFullPath();
	}


	/**
	 * 删除文件
	 * @param fileUrl 文件访问地址
	 */
	public void deleteFile(String fileUrl) {
		if (StringUtils.isEmpty(fileUrl)) {
			return;
		}
		try {
			StorePath storePath = StorePath.parseFromUrl(fileUrl);
			storageClient.deleteFile(storePath.getGroup(), storePath.getPath());
		} catch (FdfsUnsupportStorePathException e) {
			e.getMessage();
		}
	}
}
